<template>
  <div class="article-meta">
    <nuxt-link :to="{
      name: 'profile',
      params: {
        username: article.author.username
      }
    }">
      <img :src="article.author.image" />
    </nuxt-link>
    <div class="info">
      <nuxt-link class="author"
                 :to="{
                    name: 'profile',
                    params: {
                      username: article.author.username
                    }
                  }"> {{ article.author.username }} </nuxt-link>
      <span class="date">
        {{ article.createdAt | dateFormat('MMM DD, YYYY') }}
      </span>
    </div>
    <!-- 游客 -->
    <template v-if="!isAuthor">
      <button class="btn btn-sm btn-outline-secondary"
              :class="{
              active: article.author.following
            }"
              :disabled="article.author.followDisable"
              @click="onFollow(article.author)">
        <i class="ion-plus-round"></i>
        &nbsp;
        Follow {{ article.author.username }}
      </button>
      &nbsp;&nbsp;
      <button class="btn btn-sm btn-outline-primary"
              :class="{
              active: article.favorited
            }"
              :disabled="article.favoriteDisable"
              @click="onFavorite(article)">
        <i class="ion-heart"></i>
        &nbsp;
        Favorite Post
        <span class="counter">
          ({{ article.favoritesCount }})
        </span>
      </button>
    </template>

    <!-- 作者 -->
    <template v-else>
      <nuxt-link class="btn btn-outline-secondary btn-sm"
                 :to="{
                   name: 'editor',
                   query: {
                     slug: article.slug
                   }
                 }">
        <i class="ion-edit"></i> Edit Article
      </nuxt-link>
      <button class="btn btn-outline-danger btn-sm"
              @click="onDelete(article.slug)">
        <i class="ion-trash-a"></i> Delete Article
      </button>
    </template>
  </div>
</template>

<script>
export default {
  name: 'ArticleMeta',
  props: {
    article: {
      type: Object,
      required: true
    },
    isAuthor: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    onFavorite(article) {
      this.$emit('favorite', article)
    },
    onFollow(author) {
      this.$emit('follow', author)
    },
    onDelete(slug) {
      this.$emit('delete', slug)
    }
  }
}
</script>

<style>
</style>